<template>
  <div class="login">
    <div class="single-switch-lng">
      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <a title="switch language"><i class="ti ti-language ti-md"></i></a>
        <el-dropdown-menu slot="dropdown" class="pop-item-wrap pop-item-wrap-lang">
          <el-dropdown-item @click.native="changeLng('zh')">
            <a :class="lngType == 'zh' ? 'active' : ''">中文</a>
          </el-dropdown-item>
          <el-dropdown-item @click.native="changeLng('en')">
            <a :class="lngType == 'en' ? 'active' : ''">English</a>
          </el-dropdown-item>
          <!-- <el-dropdown-item>
            <a>French</a>
          </el-dropdown-item>
          <el-dropdown-item>
            <a>Arabic</a>
          </el-dropdown-item>
          <el-dropdown-item>
            <a>German</a>
          </el-dropdown-item> -->
        </el-dropdown-menu>
      </el-dropdown>
      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <a><i class="ti ti-md ti-sun"></i></a>
        <el-dropdown-menu slot="dropdown" class="pop-item-wrap pop-item-wrap-theme">
          <el-dropdown-item @click.native="changeStyle('light-style')">
            <a :class="!curStyle || curStyle == 'light-style' ? 'active' : ''"><i class="ti ti-sun me-2"></i>
              Light</a>
          </el-dropdown-item>
          <el-dropdown-item @click.native="changeStyle('dark-style')">
            <a :class="curStyle == 'dark-style' ? 'active' : ''"><i class="ti ti-moon me-2"></i> Dark</a>
          </el-dropdown-item>
          <!-- <el-dropdown-item>
            <a ><i class="ti ti-device-desktop me-2"></i> System</a>
          </el-dropdown-item> -->
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div class="wrap">
      <el-card>
        <div class="login-title">
          <img v-if="logo" :src="logo"><span>{{ this.websiteName }}</span>
        </div>
        <h4 class="mb-1 pt-2">{{$t('login.regtit')}} 🚀</h4>
        <p class="mb-4">{{$t('login.regtitdesc')}}</p>
        <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
          <!-- <h3 class="title">管理系统</h3> -->
          <el-form-item prop="username">
            <el-input v-model="registerForm.username" type="text" auto-complete="off" :placeholder="$t('plc.username')">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="registerForm.password" type="password" auto-complete="off" :placeholder="$t('plc.password')"
              @keyup.enter.native="handleRegister">
            </el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input v-model="registerForm.confirmPassword" type="password" auto-complete="off" show-password
            :placeholder="$t('plc.cfmpwd')" @keyup.enter.native="handleRegister">
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input v-model="registerForm.code" auto-complete="off" :placeholder="$t('plc.vcode')" style="width: 63%"
              @keyup.enter.native="handleRegister">
            </el-input>
            <div class="register-code">
              <img :src="codeUrl" @click="getCode" class="register-code-img" />
            </div>
          </el-form-item>
          <el-form-item style="width:100%;">
            <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
              @click.native.prevent="handleRegister">
              <span v-if="!loading">{{$t('btn.reg')}}</span>
              <span v-else>注 册 中...</span>
            </el-button>
            <div class="go-login">
              <router-link class="link-type" :to="'/login'">{{$t('btn.useacc')}}</router-link>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <!--  底部  -->
    <!-- <div class="el-register-footer">
      <span>Copyright © 2018-2024 Lion All Rights Reserved.</span>
    </div> -->
  </div>
</template>

<script>
import logoImg from '@/assets/logo/logo.png'
import { getCodeImg, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      curStyle:'',
      lngType: '',
      websiteName: 'Rent Mail',
      logo: logoImg,
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        username: [
          { required: true, trigger: "submit", message: "请输入您的账号" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "submit", message: "请输入您的密码" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: "submit", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "submit ", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {
    let _theme = localStorage.getItem('v-style');
    this.curStyle = localStorage.getItem('v-style') || 'light-style';
    this.curTheme = _theme;
    if (_theme) {
      let _htmlEl = document.getElementsByTagName('html')[0];
      if (this.isCollapse) {
        _htmlEl.setAttribute('class', 'layout-navbar-fixed layout-compact layout-menu-collapsed ' + _theme + ' layout-menu-fixed');
      } else {
        _htmlEl.setAttribute('class', 'layout-navbar-fixed layout-compact ' + _theme + ' layout-menu-fixed');
      }
    }
    this.lngType = localStorage.getItem('sys-language') || 'en';
    this.getCode();
  },
  methods: {
    changeLng(_lng) {
      localStorage.setItem('sys-language', _lng);
      this.$i18n.locale = _lng;
      this.lngType = _lng;
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            const username = this.registerForm.username;
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => { });
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          })
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  // display: flex;
  // justify-content: center;
  // align-items: center;
  // height: 100%;
  // background-image: url("../assets/image/pagebg2.png");
  // background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 1.5rem;
  padding-top: 1.5rem;
  min-height: 100vh;
  width: 100%;

  .wrap {
    position: relative;
    max-width: 400px;
    width: 100%;
    padding-top: 1.5rem;
    padding-bottom: 1.5rem;

    &:before {
      width: 238px;
      height: 233px;
      content: " ";
      position: absolute;
      top: -55px;
      left: -40px;
      background-image: url('../assets/image/login_tl_logo.svg');
    }

    &:after {
      width: 180px;
      height: 180px;
      content: " ";
      position: absolute;
      z-index: -1;
      bottom: -30px;
      right: -56px;
      background-image: url('../assets/image/login_br_logo.svg');
    }

    .el-card {
      position: relative;
      // box-shadow: 0 0.25rem 1.125rem rgba(75, 70, 92, 0.1);
      box-shadow: 0 .25rem .875rem 0 rgba(38, 43, 67, .16);
      border-radius: 0.375rem;
      background-color: #fff;

      .login-btn {
        width: 100%;
        transition: .3s all linear;

        &:hover,
        &:focus {
          background: #685dd8 !important;
          border-color: #685dd8 !important;
        }
      }

      .el-form-item__label {
        margin-bottom: 0.25rem;
        font-size: 0.8125rem;
        color: #5d596c;
        text-transform: inherit;
        letter-spacing: inherit;
        font-weight: normal;
        line-height: 15px;

        &:before {
          content: ''
        }
      }

      .el-form-item {
        margin-bottom: 1rem;

        .el-input__inner {
          // height:1.5rem;
          line-height: 1.5rem;
          border-radius: 0.375rem;
          transition: .2s all linear;
          padding-top: 0.422rem;
          padding-bottom: 0.422rem;
          font-size: 0.9375rem;
          height: inherit;

          &:focus {
            color: #6f6b7d;
            background-color: #fff;
            border-color: #7367f0;
            outline: 0;
            box-shadow: 0 0.125rem 0.25rem rgba(165, 163, 174, 0.3);
          }
        }
      }

      .remember-pwd {
        margin-bottom: 1rem;

        .el-checkbox__inner {
          width: 18px;
          height: 18px;

          &:after {
            left: 6.5px;
            top: 3px;
          }
        }

        .el-checkbox__label {
          color: #6f6b7d;
          font-weight: 500;
        }
      }

      .register-row {
        text-align: center;
        color: #6f6b7d;
        margin-top: 1rem;

        a {
          color: #7367f0;

          &:hover {
            color: #8176f2;
          }
        }
      }

      .el-form-item__error {
        width: 100%;
        margin-top: 0.25rem;
        font-size: 0.8125rem;
        color: #ea5455;
        position: relative;
      }

      .el-card__body {
        padding: 2rem;

        h4 {
          margin-bottom: 0.25rem !important;
          padding-top: 0.5rem !important;
          margin-top: 0px;
          color: #5d596c;
          font-size: 1.375rem;
          font-weight: 500;
          line-height: 1.37;
        }

        .mb-4 {
          margin-bottom: 1.5rem !important;
          margin-top: 0px;
          color: #6f6b7d;
        }
      }
    }
  }
}

.login-title {
  display: flex;
  align-items: center;
  margin-bottom: .5rem;
  justify-content: center;

  img {
    width: 50px;
  }

  span {
    border-width: 0px;
    // font-family: 'PingFangSC-Semibold', 'PingFang SC Semibold', 'PingFang SC';
    font-weight: 600;
    font-style: normal;
    font-size: 22px;
    color: #6f6b7d;
    text-align: center;
    margin-left: 0.625rem;
  }

  p {
    font-weight: 400;
    font-style: normal;
    font-size: 18px;
    color: #FFFFFF;
    text-align: center;
    line-height: 40px;
  }
}

.go-login {
  text-align: right;
  color: #6f6b7d;
  margin-top: 1rem;

  a {
    color: #7367f0;

    &:hover {
      color: #8176f2;
    }
  }
}

.register-form {
  // border-radius: 6px;
  // background: #ffffff;
  // width: 400px;
  // padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.register-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  // font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.register-code-img {
  height: 38px;
}
</style>
